package com.cn.demo4.dao.impl;

import com.cn.demo4.dao.NewsTypeDao;
import com.cn.demo4.model.NewsTypeModel;
import com.cn.demo4.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewsTypeDaoImpl implements NewsTypeDao {
    @Override
    public List<NewsTypeModel> findAll() {
        String sql ="select * from news_type order by id desc";
        ResultSet resultSet = JDBC.select(sql);
        return getData(resultSet);
    }

    @Override
    public List<NewsTypeModel> getNewsTypeModelListForHome() {
        String sql ="select * from news_type";
        ResultSet resultSet = JDBC.select(sql);
        return getData(resultSet);
    }

    private List<NewsTypeModel> getData(ResultSet resultSet) {
        List<NewsTypeModel> list = new ArrayList<>();
        try{
            while(resultSet.next()){
                NewsTypeModel newsTypeModel = new NewsTypeModel();
                newsTypeModel.setId(resultSet.getInt("id"));
                newsTypeModel.setName(resultSet.getString("name"));
                newsTypeModel.setIsShow(resultSet.getInt("is_show"));
                newsTypeModel.setStatus(resultSet.getInt("status"));
                newsTypeModel.setCreateTime(resultSet.getString("create_time"));
                newsTypeModel.setUpdateTime(resultSet.getString("update_time"));
                list.add(newsTypeModel);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

}
